function calculate() {
  const total = parseInt(document.getElementById('total').value);
  const legs = parseInt(document.getElementById('legs').value);
  const result = document.getElementById('result');

  // 判断是否合法
  if (isNaN(total) || isNaN(legs)) {
    result.textContent = "请输入有效数字";
    result.style.color = 'red';
    return;
  }

  // 计算鸡的数量
  const chickens = (4 * total - legs) / 2;
  const rabbits = total - chickens;

  // 检查是否有解
  if (chickens >= 0 && rabbits >= 0 && chickens === Math.floor(chickens) && rabbits === Math.floor(rabbits)) {
    result.textContent = `分别有鸡${chickens}只，有兔子${rabbits}只`;
    result.style.color = 'blue';
  } else {
    result.textContent = "无解";
    result.style.color = 'red';
  }
}